/**
 * Method submits form when enter is pressed
 * @param e
 * @param form
 * @returns {Boolean}
 */
function submitOnEnter(e, form)
{
    var keycode;
    if (window.event) {
        keycode = window.event.keyCode;
    } else if (e) {
        keycode = e.which;
    } else {
        return true;
    }

    if (keycode == 13) {
        form.submit();
        return false;
    } else {
        return true;
    }
}

/**
 * Email validation from stackoverflow
 * http://stackoverflow.com/questions/46155/validate-email-address-in-javascript
 * @param email
 * @returns {Boolean}
 */
function validateEmail(email) {
	var retval = false;
	if (email.length > 0) {
		var re = /^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
		retval = re.test(email);
	} else {
		retval = true;
	}
    
    return retval;
} 

/**
 * Phone number validation method
 * @param phone
 * @returns {Boolean}
 */
function validatePhone(phone) {
	var retval = false;
	//check only not null length input
	if (phone.length > 0) {
		var re = /^[0-9 \+]*$/;
		retval = re.test(phone);
	} else {
		retval = true;
	}
	
	return retval;
}

/**
 * JS to send form and check for phone nr and email
 */
function sendForm(form) {
	var comment = jQuery('#comment').val();
	var len = comment.length; 
	if (validateEmail(jQuery('#email').val()) && 
		validatePhone(jQuery('#phone').val()) &&
		len > 2) {
		form.submit();
	} else {
		alert(problemWithFeedback);
	}
}

function register(form) {
	if (validateEmail(jQuery('#email').val()) &&
		jQuery('#email').val().length > 0 &&	
		validatePhone(jQuery('#phone').val()) &&
		jQuery('#phone').val().length > 0) {
		if ($('#password').val() == $('#r_password').val()) {
			form.submit();
		} else {
			alert(passwordsMatchAlert);
		}
		
	} else {
		alert(problemWithRegister);
	}
}

function commentSearch(business) {
	//TODO: from has to be earlier than to
	var d = 0;
	if ($('#detractors').prop('checked')) {
		d = 1;
	}
	var p = 0;
	if ($('#passives').prop('checked')) {
		p = 1;
	}
	var prm = 0;
	if ($('#promoters').prop('checked')) {
		prm = 1;
	}
	$.ajax({
		  type: "POST",
		  url: prefix + '/ajax.php?page=searchComments',
		  data: { detractors: d, passives: p, promoters: prm, fromdate: $('#fromdate').val(), todate: $('#todate').val(), business: business},
		  success: function(data) {
			  $('#feedbackResults').html(data);
		  }
	});
}

function analyticsSearch(code) {
	$.ajax({
		  type: "POST",
		  url: prefix + '/ajax.php?page=searchAnalytics',
		  data: { business: jQuery('#business').val(), start: jQuery('#fromdateD').val(), end: jQuery('#todateD').val() },
		  success: function(data) {
			  $('#chartArea').html("<img src='"+prefix+"/tmp/"+code+".png'/>");
			  
		  }
	});	
}

function saveCompanyDetails() {
	$('#companyEditForm').submit();
}

function saveAccountDetails() {
	var errors = false;
	if ($('#newpassword').val() != $('#renewpassword').val()) {
		alert(passwordsMatchAlert);
		errors = true;
	}
	if ($('#password').val().length == 0) {
		alert(insertPasswordsAlert);
		errors = true;
	}
	if (validateEmail(jQuery('#email').val()) &&
			jQuery('#email').val().length > 0 &&	
			validatePhone(jQuery('#phone').val()) &&
			jQuery('#phone').val().length > 0) {
	//its actually ok :)		
			
	} else {
		alert(problemWithRegister);
		errors = true;
	}
	if (!errors) {
		$('#accountEditForm').submit();
	}
}